const fs = require("fs");
const path = require("path");
const postcss = require("postcss");
const tailwind = require("tailwindcss");

const filename = "tailwind";
const args = process.argv.slice(2);
const platform = args[0] || "h5";
const config = path.join(__dirname, `../tailwind.config.${platform}.js`);
const inputFile = path.join(__dirname, `../${filename}.src.css`);
const outputFile = path.join(__dirname, `../dist/${filename}.css`);

fs.readFile(inputFile, (err, css) => {
  if (err) throw err;
  console.log(`platform: ${platform}`);
  postcss([
    tailwind(config),
    require("../lib/remove")({
      platform: platform,
      name: args[1] || ""
    })
  ])
    .process(css, {
      from: inputFile,
      to: outputFile,
      map: { inline: false }
    })
    .then(result => {
      fs.writeFileSync(outputFile, result.css);
      if (result.map) {
        fs.writeFileSync(`${outputFile}.map`, result.map);
      }
      return result;
    })
    .then(result => {
      console.log("Built.");
    });
});
